package cn.com.clearbus.aop;


import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Aspect
public class LogAspect {

    private Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(* cn.com.clearbus.mini.controller..*.*(..))")
    public void pointCut() {
    }


    /**
     * 注意需要返回 目标函数的执行结果
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("pointCut()")
    public Object logAround(ProceedingJoinPoint pjp) throws Throwable {
        long beginTime = System.currentTimeMillis();
        Object target = pjp.getTarget();
        ApiOperation apiOperation = target.getClass().getAnnotation(ApiOperation.class);
        if (apiOperation != null) {
            logger.info("开始执行{}类的..{}..方法,时间:{}",target.getClass().getName(),apiOperation.value(),beginTime);
        }else{
            logger.info("开始执行{}类的..{}..方法,时间:{}",target.getClass().getName(),pjp.getSignature().getName(),beginTime);
        }
        logger.info("请求参数信息为:{}",pjp.getArgs());
        Object resut = pjp.proceed();
        logger.info("结束请求,耗时:{}ms",(System.currentTimeMillis()-beginTime)/1000);
        return resut;
    }

    /**
     * 异常返回
     */
    @AfterThrowing(pointcut = "pointCut()",throwing="ex")
    public void logException(Throwable ex) {
        logger.info("目标方法中抛出的异常:" + ex);
        logger.info("模拟Advice对异常的修复...");
    }

}
